package com.huatai.user.controller;

import com.huatai.common.api.R;
import com.huatai.common.log.annotation.Log;
import com.huatai.common.log.enums.BusinessType;
import com.huatai.user.entity.Department;
import com.huatai.user.entity.Post;
import com.huatai.user.service.DepartmentService;
import com.huatai.user.service.PostService;
import com.huatai.user.vo.DepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: ht-system
 * @description: 部门岗位管理
 * @author: songxiude
 * @create: 2023-02-17 09:44
 **/
@RestController
@RequestMapping("/dept")
@Api(value = "部门岗位管理", tags = "部门岗位管理")
public class DeptPostController {
	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private PostService postService;

	@PostMapping("/saveDept")
	@ApiOperation(value = "添加修改部门", notes = "添加修改部门")
	@Log(title = "添加修改部门",businessType = BusinessType.INSERT)
	public R saveDept(@Valid @RequestBody Department department){
		return R.status(departmentService.saveDepartment(department));
	}

	@PostMapping("/delDept")
	@Log(title = "删除部门",businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除部门", notes = "删除部门（如果该部门包含子级部门请从最子级往上删除）")
	public R delDept(@RequestParam("dept_id") Long deptId){
		return R.status(departmentService.delDept(deptId));
	}

	@GetMapping("/getDeptList")
	@ApiOperation(value = "递归获取部门列表", notes = "递归获取部门列表")
	public R<List<DepartmentVo>> getDeptList(@ApiParam(value = "显示状态",name = "status") @RequestParam("status") Integer status){
		return R.data(departmentService.getDeptList(status));
	}

	@PostMapping("/savePost")
	@ApiOperation(value = "岗位新增或更新", notes = "岗位新增或更新")
	@Log(title = "岗位新增或更新",businessType = BusinessType.UPDATE)
	public R savePost(@Valid @RequestBody Post post){
		return R.status(postService.savePost(post));
	}

	@PostMapping("/delPost")
	@ApiOperation(value = "删除岗位", notes = "删除岗位")
	@Log(title = "删除岗位",businessType = BusinessType.DELETE)
	public R delPost(@ApiParam(name = "岗位id",value = "post_id",required = true) @RequestParam("post_id") Long postId){
		//Assert.isNull(postId,"岗位id不能为空");
		return R.status(postService.delPost(postId));
	}

	@GetMapping("/getPostList")
	@ApiOperation(value = "获取岗位列表", notes = "获取岗位列表")
	public R<List<Post>> getPostList(@ApiParam(value = "显示状态",name = "status") @RequestParam("status") Integer status){
		return R.data(postService.getPostList(status));
	}

	@PostMapping("/listByDepartName")
	@ApiOperation(value = "部门成员列表",notes = "部门成员列表")
	public R listByDepartName(String departName){
		return R.data(postService.listByDepartName(departName));
	}

}
